<?php
## include required files
/*******************************/

require_once '../../includefiles.php';
require_once '../../model/users.php';

if($_GET['id1']!='msg' && $_GET['id2']!='open')
{

	if($_SESSION['getbids_id']!='')
	{
		header("Location: ".SITE_URL."/");
		exit;		
	}
}

## Create Objects
/*******************************/
$userObj = new Model_Users();
/*******************************/

$retPath  = $_SESSION['return']; 

if(isset($_POST['signin']))
{   
    //echo"ggg";exit;
	$unm      = trim($_POST['unm']);
	$pw       = trim($_POST['pw']);
	$rememberMe		= return_fetched_value($_POST['rememberMe']);	
	#echo $rememberMe ;exit;
	$result   = $userObj->getUserValueByDetailsUsernameAndPasswordforTradesman($unm,$pw);
	$cnt      = count($result);
  	$retPath  = $_SESSION['return'];
    if($cnt > 0)
	{
		if($result['user_status']!='0')
		{
				  $_SESSION['getbids_id']  = $result['id'];
				  $_SESSION['first_name']      = $result['first_name'];
				  $_SESSION['last_name']       = $result['last_name'];
				  $_SESSION['user_type']       = $result['user_type'];
				  $_SESSION['email']           = $result['email'];
				  
				 if($rememberMe != '')
				 {
						$hour =time() + 3600 * 24 * 30; 
						setcookie(USERNAME_COOKIE, $_POST['unm'], $hour);
						setcookie(PASSWORD_COOKIE, $_POST['pw'], $hour);   
						#setcookie("email", $_POST['email'], time()+3600*24);
						#setcookie("password", $_POST['password'], time()+3600*24);
				 }
					
				if($result['login_fisrttime']=='0')
				{	
					$Userarray['login_fisrttime']=1;
					$userObj->editUserValueById($Userarray,$_SESSION['getbids_id']);
					echo '<script language="JavaScript" type="text/JavaScript">location.href ="'.SITE_URL.'/signin/msg/open/signin/"</script>';
				}
				else
				{
					  if($retPath != ""){
						redirect_to_link("".$retPath."");
					  }else{
						redirect_to_link("".SITE_URL."/leads/");  
					  }
				}
		} 
		else 
		{		
			$_SESSION['error'] = '<div class="error">Your account is inactive! Please contact your site administrator. </div>';
			echo '<script language="JavaScript" type="text/JavaScript">location.href ="'.SITE_URL.'/signin/error/102/signin"</script>';
		}
	}
	else
	{
		 $_SESSION['error'] = '<div class="error">Invalid Username Or password !</div>';
		echo '<script language="JavaScript" type="text/JavaScript">location.href ="'.SITE_URL.'/signin/error/101/signin"</script>';
	}
}

if(isset($_POST['signinhomeowner']))
{   
    //echo"ggg";exit;
	$username      = trim($_POST['username']);
	$password       = trim($_POST['password']);
	$rememberMe		= return_fetched_value($_POST['rememberMehomeowner']);	
	#echo $rememberMe ;exit;
	$result   = $userObj->getUserValueByDetailsUsernameAndPasswordforHome($username,$password);
	$cnt      = count($result);
  	$retPath  = $_SESSION['return'];
    if($cnt > 0)
	{
		if($result['user_status']!='0')
		{
				  $_SESSION['getbids_id']  = $result['id'];
				  $_SESSION['first_name']      = $result['first_name'];
				  $_SESSION['last_name']       = $result['last_name'];
				  $_SESSION['user_type']       = $result['user_type'];
				  $_SESSION['email']           = $result['email'];
				  
				 if($rememberMe != '')
				 {
						$hour =time() + 3600 * 24 * 30; 
						setcookie(HOMEOWNER_USERNAME_COOKIE, $_POST['username'], $hour);
						setcookie(HOMEOWNER_PASSWORD_COOKIE, $_POST['password'], $hour);   
						#setcookie("email", $_POST['email'], time()+3600*24);
						#setcookie("password", $_POST['password'], time()+3600*24);
				 }
					
				if($result['login_fisrttime']=='0')
				{	
					$Userarray['login_fisrttime']=1;
					$userObj->editUserValueById($Userarray,$_SESSION['getbids_id']);
					echo '<script language="JavaScript" type="text/JavaScript">location.href ="'.SITE_URL.'/signin/msg/open/signin"</script>';
				}
				else
				{
					  if($retPath != ""){
						redirect_to_link("".$retPath."");
					  }else{
						redirect_to_link("".SITE_URL."/myaccount/");  
					  }
				}
		} 
		else 
		{		
			$_SESSION['error'] = '<div class="error">Your account is inactive! Please contact your site administrator.</div>';
			echo '<script language="JavaScript" type="text/JavaScript">location.href ="'.SITE_URL.'/signin/error/202/signin"</script>';
		}
	}
	else
	{
		 $_SESSION['error'] = '<div class="error">Invalid Username Or password !</div>';
		echo '<script language="JavaScript" type="text/JavaScript">location.href ="'.SITE_URL.'/signin/error/201/signin"</script>';
	}
}
// for tradesman user
if(isset($_COOKIE['USERNAME_COOKIE']) && isset($_COOKIE['PASSWORD_COOKIE'])){
	$User_name =$_COOKIE['USERNAME_COOKIE'];
	$User_pass =$_COOKIE['PASSWORD_COOKIE'];
}else{
	$User_name="";
	$User_pass="";
}
$smarty->assign("User_name", $User_name);	
$smarty->assign("User_pass", $User_pass);	

// for homeowner user
if(isset($_COOKIE['HOMEOWNER_USERNAME_COOKIE']) && isset($_COOKIE['HOMEOWNER_PASSWORD_COOKIE'])){
	$Homeowner_User_name =$_COOKIE['HOMEOWNER_USERNAME_COOKIE'];
	$Homeowner_User_pass =$_COOKIE['HOMEOWNER_PASSWORD_COOKIE'];
}else{
	$Homeowner_User_name="";
	$Homeowner_User_pass="";
}
$smarty->assign("Homeowner_User_name", $Homeowner_User_name);	
$smarty->assign("Homeowner_User_pass", $Homeowner_User_pass);	


if($_GET['id1']=="msg" && $_GET['id2']==101){	
	     $_SESSION['sucmsg'] = "<div class='success_msg'><span>Sign Out Successfully</span></div>"; 
	     $smarty->assign("successMsg", $_SESSION['sucmsg']);	
	     unset($_SESSION['sucmsg']);
}

if($_GET['id1']=="error" && $_GET['id2']==101){
	$_SESSION['error'] = 'Invalid Username and password.';
	$smarty->assign("errorMsg", $_SESSION['error']);	
	unset($_SESSION['error']);
}

if($_GET['id1']=="error" && $_GET['id2']==102){
	$_SESSION['error'] = 'Your account is inactive! Please contact your site administrator.';
	$smarty->assign("errorMsg", $_SESSION['error']);	
	unset($_SESSION['error']);
}

if($_GET['id1']=="error" && $_GET['id2']==201){
	$_SESSION['error'] = 'Invalid Username and password.';
	$smarty->assign("errorMsgHome", $_SESSION['error']);	
	unset($_SESSION['error']);
}

if($_GET['id1']=="error" && $_GET['id2']==202){
	$_SESSION['error'] = 'Your account is inactive! Please contact your site administrator.';
	$smarty->assign("errorMsgHome", $_SESSION['error']);	
	unset($_SESSION['error']);
}

if($_SESSION['sucmsg']!=''){
	$smarty->assign("successMsg", $_SESSION['sucmsg']);
	unset($_SESSION['sucmsg']);
}

//Active Class
$active = 'login';
$smarty->assign('active', $active);

$smarty->display(TEMPLATEDIR . '/modules/signin/signin.tpl');
?>